<?php

/*

	Copyright © 2011 Mike Bennett <mike@kereru.com>

	This file is part of Tazu-Time.

    Tazu-Time is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    Tazu-Time is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tazu-Time.  If not, see <http://www.gnu.org/licenses/>.
    
*/



class edit_project  {

	var $title = FALSE;
	var $name = 'edit_project';
	var $entry = FALSE;

	var $task_id = FALSE;
	var $db_file;

	var $period = FALSE;			// Period combo widget
	var $calendar1 = FALSE;			// Calendar widget

	function __construct($title="Edit Project",$name=FALSE) {
		$this->title = $title;	
		if ( $name ) $this->name = $name;
	}


	function edit($customer_id,$task_id,$db_file=FALSE) {

		// $customer_id == FALSE add new record

		$this->task_id = $task_id;
		$this->db_file = $db_file;
		if ( ! $db_file ) $this->db_file = platform::app_data('Tazu-Timesheet-Manager').'customers.db';

		// label is above the text entry
		// prompt is to the left
		// err_msg is below

		$cnf = new conf_timesheet(INIFILE);

		$glade = new GladeXML(PATH_RES.'edit_project.glade','edit_project');
		$glade->signal_autoconnect_instance($this);
		$dialog = $glade->get_widget('edit_project');
		$dialog->set_title($this->title);

		$labels = array('code_lbl','description_lbl','estimate_lbl','proj_charged');
		foreach ( $labels as $lbl ) {
			$w = $glade->get_widget($lbl);
			$w->set_label(settings::xlate($w->get_label()));
		}		



		$_code = $glade->get_widget('proj_edit_code');
		$_description = $glade->get_widget('proj_edit_description');
		$_est = $glade->get_widget('proj_edit_est');
		$_est_min = $glade->get_widget('proj_edit_est_min');
		$_warn = $glade->get_widget('proj_edit_warn');
		$_charged = $glade->get_widget('proj_charged');
		$_rate = $glade->get_widget('proj_rate');
		$this->period = $glade->get_widget('proj_period');
		$this->calendar1 = $glade->get_widget('calendar1');	// billing date for one=off charges/quotes


		// Defaults
		$code = '';
		$description = '';
		$hh = 0;
		$mm = 0;
		$estimate_warning = 0;
		$charged = TRUE;
		$rate = 0;
		$period = 0;
		$bill_date = 0;

		// Edit an existing customer record
		if ( $this->task_id ) {
			$ts = new Customers($this->db_file);
			$code = $ts->getProjectCode($task_id);
			$description = $ts->getProjectDescription($task_id);

			$estimate_time = $ts->elapsed($ts->getProjectField($task_id,'estimate_time'));
			$parts = explode(":",$estimate_time);
			$hh = $parts[0];
			$mm = $parts[1];

			$estimate_warning = $ts->getProjectField($task_id,'estimate_warning');

			$charged = $ts->getProjectField($task_id,'charged');
			$rate = sprintf("%01.2f",$ts->getProjectField($task_id,'rate'));
			$period = $ts->getProjectField($task_id,'period');
			$bill_date = $ts->getProjectField($task_id,'bill_date') * 1 ;

			$ts->close();
			unset($ts);
			
		}


		if ( $bill_date == 0 ) $bill_date = mktime();			// today
			
		$_code->set_text($code);
		$_description->set_text($description);
		$_est->set_value($hh);
		$_est_min->set_value($mm);
		$_warn->set_value(floatval($estimate_warning));
		$_charged->set_active($charged);
		$_rate->set_value($rate);

		// Combo
		// Create a model
		if (defined("GObject::TYPE_STRING")) {
		    $model = new GtkListStore(GObject::TYPE_STRING);
		} else {
		    $model = new GtkListStore(GObject::TYPE_STRING);
		}
		$this->period->set_model($model);
		$cellRenderer = new GtkCellRendererText(); // note 2
		$this->period->pack_start($cellRenderer);
		$this->period->set_attributes($cellRenderer, 'text', 0); // note 3 
		$model->append(array('per hour'));
		$model->append(array('per period'));
		$model->append(array('quote'));
		$this->period->set_active($period);

		// Billing date
		$this->calendar1->select_month(date('m',$bill_date)-1,date('Y',$bill_date));
		$this->calendar1->select_day(date('d',$bill_date));
		$this->calendar1->mark_day(date('d',$bill_date));

		if ( $period == 2 ) {			// one off and quotes
			$this->calendar1->set_sensitive(TRUE);
		}
		else {
			$this->calendar1->set_sensitive(FALSE);
		}

//		$this->entry = $glade->get_widget('entry');
//		$this->entry->set_width_chars($width_chars);


        $buttons = $dialog->action_area->get_children();
        $button_ok = $buttons[1]; // get the ID of the OK button

        // simulate button click when user press enter
/*
		$this->entry->set_text($default);
        $this->entry->connect('activate', array(&$this, 'on_enter'), $button_ok);
*/

        $dialog->show_all();
        $result = $dialog->run();

		//echo "+ ".Gtk::RESPONSE_OK."\n";
		//echo "- ".Gtk::RESPONSE_CANCEL."\n";
		//echo "->[$result]\n";

		if ( $result == Gtk::RESPONSE_OK ) {

			$code = $_code->get_text();
			$description = $_description->get_text();
			$hh = $_est->get_value();
			$mm = $_est_min->get_value();
			$estimate_time = ($hh*3600)+($mm*60);
			$estimate_warning = $_warn->get_value();
			
			$charged = $_charged->get_active();
			$rate = $_rate->get_value();
			$period = $this->period->get_active();
			$calendar1 = $this->calendar1->get_date();

//print_r($calendar1);

			$bill_date = mktime(0,0,0,$calendar1[1]+1,$calendar1[2],$calendar1[0]);

			$ok = TRUE;			// Need validation
			if ( $ok ) {
					
				$ts = new Customers($this->db_file);

				if ( $this->task_id ) {
					// Existing record
					$ts->updateProjectCode($task_id,$code);							
					$ts->updateProjectDescription($task_id,$description);							
					$ts->updateTask($task_id,'estimate_time',$estimate_time);							
					$ts->updateTask($task_id,'estimate_warning',$estimate_warning);							
					$ts->updateTask($task_id,'charged',$charged);							
					$ts->updateTask($task_id,'rate',$rate);							
					$ts->updateTask($task_id,'period',$period);							
					$ts->updateTask($task_id,'bill_date',$bill_date);							
				}
				else {
					// New record
					$task_id = $ts->addProject($customer_id,$code,$description,$estimate_time,$estimate_warning,$charged,$rate,$period);
					$ts->updateTask($task_id,'bill_date',$bill_date);							
				}
						
				$ts->close();
				unset( $ts );

			}


			$result = $task_id;

		}
		else {

			$result = FALSE;		// cancel
		}

        $dialog->destroy();

		return($result);

    }

	/**
	*	Period combo has changed
	*	
	*	@param	
	*	@return	
	*	
	*/
	function onPeriodChanged() {
		$period = $this->period->get_active();
		if ( $period == 2 ) {			// one off and quotes
			$this->calendar1->set_sensitive(TRUE);
		}
		else {
			$this->calendar1->set_sensitive(FALSE);
		}
	}

	// simulate button click when user press enter
    function on_enter($entry, $button) {
        $button->clicked();
    }

}

?>